<?php
class My_View_Helper_CategoryPositionHelper extends Zend_View_Helper_Abstract
{
    protected $_category;
    protected $_marginLeft;
    protected $_user;

    public function __construct() {
        $this->_category = new Admin_Model_Category();
        $this->_user     = Zend_Auth::getInstance()->getIdentity();
    }    

    public function categoryPositionHelper() {      
        $this->_buildCategoryTree();
    }

    protected function _buildCategoryTree() {        
        $allParents = array();
        $availableCategories = $this->_category->getAvailableForRoleCategories($this->_user->roleId);
        foreach ($availableCategories as $category) {
            $rootCategory = $this->_findRootCategoryForCurrentRole(
                $category['id'], 
                $category['parent_id']
            );
            if (!is_null($rootCategory)) {
                if (!in_array($rootCategory, $allParents))
                    $allParents[] = $rootCategory;
            }
        }
        $isFirst = true;
        $allParentsString = '';
        foreach ($allParents as $parentId) {
            $comma = !$isFirst?',':'';
            $allParentsString .= $comma . $parentId;
            $isFirst = false;
        }
        
        $parents = $this->_category->getAllCategoriesWhereIdIn($allParentsString);        
        $menu = '';
        foreach ($parents as $parent) {  
            $this->_marginLeft += 20;                            
            $menu .= $this->_buildNode($parent, count($parents));            
            $this->_marginLeft = 0;  
        }        
        echo $menu;
    }

    protected function _buildNode(array $parent, $currentCountParents) {  
        $totalCountParents = $this->_category->getTotalCategoryCountsByParentId(
            $parent['parent_id']
        );
        $disable = $totalCountParents != $currentCountParents?1:0;
        $style   = $disable?'color:red;':'color:black;';
        $node  = '<li id="id_' . $parent['id'] . '" 
                      parent="'.$parent['parent_id'].'"
                      disable="'.$disable.'"
                      numeric_id="'.$parent['id'].'"
                      style="margin-left: ' . $this->_marginLeft .'px;'.$style.'">';
        $node .= '<div>' . $parent['category_name'] . '</div>';
        $node .= $this->_getChildNodes($parent['id']);        
        $node .= '</li>';        
        return $node;
    }

    protected function _getChildNodes($parentId) {          
        $children = $this->_category->getAvailableForRoleCategoriesByParentId(
            $this->_user->roleId, 
            $parentId
        );
        $childNode = '';
        if ($children) {                    
            $childNode .= '<ol>';
            foreach ($children as $child) {
                $childNode .= $this->_buildNode($child, count($children));
            }
            $childNode .= '</ol>';
        }
        return $childNode;
    }

    protected function _findRootCategoryForCurrentRole($categoryId, $parentCategoryId) {        
        if (!$parentCategoryId)
            return $categoryId;

        $parentParentCategoryId = $this->_category->checkAvailabilityCategory(
            $this->_user->roleId,
            $parentCategoryId         
        );

        if (!$parentParentCategoryId)
            return $parentCategoryId;

        $this->_findRootCategoryForCurrentRole($parentCategoryId, $parentParentCategoryId);
    }    
}